﻿using System;
using System.Collections;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using MySql.Data.MySqlClient;
using System.Data;
using System.Configuration;

/// <summary>
///WebServiceJp 的摘要说明
/// </summary>
[WebService(Namespace = "http://chilcoo.com/cws/jp/open/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebServiceJp : System.Web.Services.WebService {

    private MySqlConnection conn = null;
    private MySqlCommand command = null;
    //声明Soap头实例
    public CSoapHeader CHeader = new CSoapHeader();

    public WebServiceJp()
    {
        this.conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["cws_mysql"].ConnectionString);
    }
    [WebMethod(Description="日本郵便番号で住所を検索する")]
    public string ZipCode(string zip) {
        
        String[] objs = new String[] { "", "", "" };
        string result = "\"ken\":\"{0}\",\"city\":\"{1}\",\"address\":\"{2}\"";
        //可以通过存储在数据库中的用户与密码来验证
        // CHeader.UserName.Equals("houlei")
        // token ture is check user and passwd,false is not check;
        zip = zip.Replace("-", "");
        try
        {
            string sql = "SELECT zip,ken,city,addr1,mult_id FROM cs2_zip WHERE (zip='" + zip + "');";
            this.command = new MySqlCommand(sql, this.conn);
            MySqlDataAdapter dataAdapter = new MySqlDataAdapter(this.command);
            DataTable table = new DataTable();
            DataRow row = null;
            dataAdapter.Fill(table);
            
            if (table.Rows.Count > 0)
            {
                row = table.Rows[0];
            }
            if (row != null)
            {
                objs[0] = row["ken"].ToString();
                objs[1] = row["city"].ToString();
                objs[2] = row["addr1"].ToString();
            }

            result = "{" + String.Format(result, objs) + "}";
        }
        catch (MySqlException se)
        {
            result = "{ref: " + se.StackTrace + "}";
        }
        
        return result;
    }

    [WebMethod(Description = "日本郵便番号で住所を検索する[XML]")]
    public String[] ZipCodeXml(string zip)
    {

        String[] objs = new String[] { "", "", "" };
        //可以通过存储在数据库中的用户与密码来验证
        // CHeader.UserName.Equals("houlei")
        // token ture is check user and passwd,false is not check;
        zip = zip.Replace("-", "");
        try
        {
            string sql = "SELECT zip,ken,city,addr1,mult_id FROM cs2_zip WHERE (zip='" + zip + "');";
            this.command = new MySqlCommand(sql, this.conn);
            MySqlDataAdapter dataAdapter = new MySqlDataAdapter(this.command);
            DataTable table = new DataTable();
            DataRow row = null;
            dataAdapter.Fill(table);

            if (table.Rows.Count > 0)
            {
                row = table.Rows[0];
            }
            if (row != null)
            {
                objs[0] = row["ken"].ToString();
                objs[1] = row["city"].ToString();
                objs[2] = row["addr1"].ToString();
            }
        }
        catch (MySqlException){
        }

        return objs;
    }
    
}

